package com.gtw.modules.api.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.dto.TbProxyDetailsDto;
import com.gtw.modules.api.service.TbCommodityService;
import com.gtw.modules.api.service.TbOrderService;
import com.gtw.modules.api.service.TbShoppingCartServices;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.TbProxyDetailsService;
import com.gtw.modules.sys.service.TbProxyDividendDetailsService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 代理佣金分红Controller
 * Created by wuye on 2018/3/21.
 */
@RestController
@RequestMapping("/api/proxy")
public class TbProxyController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbProxyDetailsService tbProxyDetailsService;

    @Autowired
    private TbProxyDividendDetailsService tbProxyDividendDetailsService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbCommodityService tbCommodityService;


    /**
     * 获取代理佣金明细
     *
     * @return
     */
    @RequestMapping("/foundBycommission")
    public R foundBycommission(@LoginUserApi TbUser tbUser) {
        List<TbProxyDetails> tbProxyDetails = tbProxyDetailsService.queryBybenefit(tbUser.getUserId());
        return R.ok().put("data", tbProxyDetails);
    }

    /**
     * 获取代理分红明细
     *
     * @return
     */
    @RequestMapping("/foundByDividend")
    public R foundByDividend(@LoginUserApi TbUser tbUser) {
        List<TbProxyDividendDetails> tbProxyDividendDetails = tbProxyDividendDetailsService.queryBybenefit(tbUser.getUserId());
        return R.ok().put("data", tbProxyDividendDetails);
    }


    /**
     * 个人佣金详情
     * 本月预计收入
     * 本月销量
     * 总销量
     * 团队成员
     *
     * @return
     */
    @RequestMapping("/foundByDetailed")
    public R foundByDetailed(@LoginUserApi TbUser tbUser) {
        //查询本月佣金明细


        //查询所有佣金明细


        //查询团队成员

        return R.ok();
    }

    /**
     * 加载代理销量(提成)列表
     *
     * @return 时间 金额 触发用户 来源  商品 朋友等级
     * @Todo判断用户是我的几级朋友
     */
    @RequestMapping("/foundByProxyCommissionList")
    public R foundByProxyCommissionList(@LoginUserApi TbUser tbUser, @RequestParam Map<String, Object> params) {
        List<TbProxyDetailsDto> dtoList = new ArrayList<>();
        //是否加载本月 反之加载所有
        int ismonth = Integer.valueOf(params.get("ismonth").toString());
        List<TbProxyDetails> monthList = new ArrayList<>();
        if (ismonth == 1) {
            params.put("benefituserId", tbUser.getUserId());
            Query query = new Query(params);
            //加载本月数据
            monthList = tbProxyDetailsService.queryByThisMonth(query);
            for (TbProxyDetails tpd : monthList) {
                TbProxyDetailsDto dto = new TbProxyDetailsDto();
                dto.setCreatetime(tpd.getCreatetime());
                dto.setMoney(tpd.getAmount());
                TbUser targetUser = tbUserService.queryById(tpd.getTriggerUserid());
                targetUser.setPassword("");
                targetUser.setPaypwd("");
                dto.setTargetUser(targetUser);
                dtoList.add(dto);
            }
        } else {
            //加载全部数据
            params.put("benefituserId",tbUser.getUserId());
            Query query = new Query(params);
            monthList = tbProxyDetailsService.queryByThisAll(query);
            for (TbProxyDetails tpd : monthList) {
                TbProxyDetailsDto dto = new TbProxyDetailsDto();
                dto.setCreatetime(tpd.getCreatetime());
                dto.setMoney(tpd.getAmount());
                if (!tpd.getTriggerUserid().equals(null)) {
                    TbUser targetUser = tbUserService.queryById(tpd.getTriggerUserid());
                    if (targetUser != null) {
                        targetUser.setPassword("");
                        targetUser.setPaypwd("");
                        dto.setTargetUser(targetUser);
                        dtoList.add(dto);
                    }
                }

            }
        }
        //指定下级用户ID
        if (params.get("nextid") != null) {
            Integer nextid = Integer.valueOf(params.get("nextid").toString());
            Map<String, Object> map = new HashedMap();
            map.put("benefituserId", tbUser.getUserId());
            map.put("triggerUserid", nextid);
            //该用户给我打来的收益
            monthList = tbProxyDetailsService.queryBytrigger(map);
            for (TbProxyDetails tpd : monthList) {
                TbProxyDetailsDto dto = new TbProxyDetailsDto();
                dto.setCreatetime(tpd.getCreatetime());
                dto.setMoney(tpd.getAmount());
                TbUser targetUser = tbUserService.queryById(tpd.getTriggerUserid());
                targetUser.setPassword("");
                targetUser.setPaypwd("");
                dto.setTargetUser(targetUser);
                dtoList.add(dto);
            }
        }
          return R.ok().put("data", dtoList);
    }

    /**
     * 加载用户的佣金收入列表(会员)
     *
     * @return
     */
    @RequestMapping("/foundByMemberList")
    public R foundByMemberList(@LoginUserApi TbUser tbUser, @RequestParam Map<String, Object> params) {
        List<TbUser> userList = new ArrayList<>();
        List<TbUser> userList1 = tbUserService.queryByParentId(tbUser.getUserId());
        for (TbUser tbUser1 : userList1) {
            userList.add(tbUser1);
            List<TbUser> userList2 = tbUserService.queryByParentId(tbUser1.getUserId());
            for (TbUser tbUser2 : userList2) {
                userList.add(tbUser2);
                List<TbUser> userList3 = tbUserService.queryByParentId(tbUser2.getUserId());
                for (TbUser tbUser3 : userList3) {
                    userList.add(tbUser3);
                    List<TbUser> userList4 = tbUserService.queryByParentId(tbUser3.getUserId());
                    for (TbUser tbUser4 : userList4) {
                        userList.add(tbUser4);
                    }
                }
            }
        }
        return R.ok().put("data", userList);
    }
}
